﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SYcwpb.Views;
using SYcwpb.Access;
using SYcwpb.Commands;
using System.Windows.Input;
using System.Windows.Media;
using SYcwpb.Helpers;
using System.ComponentModel;

namespace SYcwpb.ViewModels
{
    class CrossingRoadColorConfigViewModel : ViewModelBase, IDataErrorInfo
    {
        private CrossingRoadColorConfigView _view = null;
        private CrossingRoadColorConfigAccess _access = null;

        private ColorConfig _colorConfig = null;

        private ICommand _saveCommand = null;

        public string MaxMilesZB
        {
            get { return _colorConfig.WarnMilesZB.ToString(); }
            set
            {
                if (value == _colorConfig.WarnMilesZB.ToString())
                    return;

                _colorConfig.WarnMilesZB = Convert.ToDouble(value);
                OnPropertyChanged("MaxMilesZB");
            }
        }

        public string MaxMilesBB
        {
            get { return _colorConfig.WarnMilesBB.ToString(); }
            set
            {
                if (value == _colorConfig.WarnMilesBB.ToString())
                    return;

                _colorConfig.WarnMilesBB = Convert.ToDouble(value);
                OnPropertyChanged("MaxMilesBB");
            }
        }

        public string MaxMilesYB
        {
            get { return _colorConfig.WarnMilesYB.ToString(); }
            set
            {
                if (value == _colorConfig.WarnMilesYB.ToString())
                    return;

                _colorConfig.WarnMilesYB = Convert.ToDouble(value);
                OnPropertyChanged("MaxMilesYB");
            }
        }

        public string MaxWhoursZB
        {
            get { return _colorConfig.WarnWhourZB.ToString(); }
            set
            {
                if (value == _colorConfig.WarnWhourZB.ToString())
                    return;

                _colorConfig.WarnWhourZB = Convert.ToDouble(value);
                OnPropertyChanged("MaxWhoursZB");
            }
        }

        public string MaxWhoursBB
        {
            get { return _colorConfig.WarnWhourBB.ToString(); }
            set
            {
                if (value == _colorConfig.WarnWhourBB.ToString())
                    return;

                _colorConfig.WarnWhourBB = Convert.ToDouble(value);
                OnPropertyChanged("MaxWhoursBB");
            }
        }

        public string MaxWhoursYB
        {
            get { return _colorConfig.WarnWhourYB.ToString(); }
            set
            {
                if (value == _colorConfig.WarnWhourYB.ToString())
                    return;

                _colorConfig.WarnWhourYB = Convert.ToDouble(value);
                OnPropertyChanged("MaxWhoursYB");
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (null == _saveCommand)
                {
                    _saveCommand = new RelayCommands(
                        Para => this.Save(),
                        Para => this.CanSave()
                        );
                }
                return _saveCommand;
            }
        }

        public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                string error = null;
                switch (columnName)
                {
                    case "MaxMilesZB":
                        error = Validate(this.MaxMilesZB);
                        break;
                    case "MaxMilesBB":
                        error = Validate(this.MaxMilesBB);
                        break;
                    case "MaxMilesYB":
                        error = Validate(this.MaxMilesYB);
                        break;
                    case "MaxWhoursZB":
                        error = Validate(this.MaxWhoursZB);
                        break;
                    case "MaxWhoursBB":
                        error = Validate(this.MaxWhoursBB);
                        break;
                    case "MaxWhoursYB":
                        error = Validate(this.MaxWhoursYB);
                        break;
                }
                return error;
            }
        }

        public CrossingRoadColorConfigViewModel(CrossingRoadColorConfigView view,ColorConfig colorConfig)
        {
            this._view = view;

            this._access = new CrossingRoadColorConfigAccess();

            this._colorConfig = colorConfig;

            _view.ColorPickerMiles.SelectedColor = _colorConfig.WarnColorMiles;

            _view.ColorPickerWhours.SelectedColor = _colorConfig.WarnColorWhours;
        }

        private string Validate(string validateValue)
        {
            string error = null;
            if (string.IsNullOrEmpty(validateValue))
            {
                error = "不能为空";
            }
            else if (!GlobalVariables.IsDouble(validateValue))
            {
                error = "请使用有效数字输入";
            }
            return error;
        }

        private bool CanSave()
        {
            return string.IsNullOrEmpty(Validate(MaxMilesZB)) && string.IsNullOrEmpty(Validate((MaxMilesBB))) && string.IsNullOrEmpty(Validate(MaxMilesYB))
                && string.IsNullOrEmpty(Validate(MaxWhoursZB)) && string.IsNullOrEmpty(Validate((MaxWhoursBB))) && string.IsNullOrEmpty(Validate(MaxWhoursYB));
        }

        private void Save()
        {
            string err = null;
            _colorConfig.WarnColorMiles = _view.ColorPickerMiles.SelectedColor;
            _colorConfig.WarnColorWhours = _view.ColorPickerWhours.SelectedColor;

            if (!_access.SaveConfig(this._colorConfig, ref err))
                MessageBoxHelper.ShowError(string.Format("保存异常：{0}", err));
            else
            {
                MessageBoxHelper.ShowTips("保存交路警示设置成功");

                _view.OnCommited();
            }
        }
    }

    public class ColorConfig
    {
        public static Color DefaultColor = Colors.Tan;
        public static double DefaultMax = 99999.0;

        public double WarnMilesZB { get; set; }

        public double WarnMilesBB { get; set; }

        public double WarnMilesYB { get; set; }


        public double WarnWhourZB { get; set; }

        public double WarnWhourBB { get; set; }

        public double WarnWhourYB { get; set; }

        public Color WarnColorMiles { get; set; }

        public Color WarnColorWhours { get; set; }

        public ColorConfig(){
            this.WarnMilesZB = this.WarnMilesBB = this.WarnMilesYB = this.WarnWhourZB = this.WarnWhourBB = this.WarnWhourYB = ColorConfig.DefaultMax;

            this.WarnColorMiles = this.WarnColorWhours = ColorConfig.DefaultColor;
        }

    }
}
